package com.gengzp.mq.server.service.impl;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.gengzp.mq.common.model.entity.GengzpMqMessage;
import com.gengzp.mq.common.model.resp.SearchMessageCountResp;
import com.gengzp.mq.common.utils.GengzpMqDateUtils;
import com.gengzp.mq.server.facade.IGengzpMqFacadeService;
import com.gengzp.mq.server.service.IGengzpMqDashboardService;
import com.google.common.collect.Maps;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName GengzpMqDashboardServiceImpl
 * @Description
 * @Author gengzp
 * @Date 2025/7/23 15:27
 */
@Service
public class GengzpMqDashboardServiceImpl implements IGengzpMqDashboardService {

    @Autowired
    private IGengzpMqFacadeService gengzpMqFacadeService;

    @Override
    public List<SearchMessageCountResp> searchMessageCount() {
        List<SearchMessageCountResp> result = Lists.newArrayList();

        // 获取当前系统时间
        Date currentDate = new Date();

        // 往前查询10小时
        List<GengzpMqMessage> gengzpMqMessages = gengzpMqFacadeService.searchMessageByCreatedTime(
                new Date(currentDate.getTime() - 10 * 60 * 60 * 1000), currentDate);
        if (CollectionUtils.isEmpty(gengzpMqMessages)) {
            return result;
        }

        // 2小时前
        Date beforeTwoHours = new Date(currentDate.getTime() - 2 * 60 * 60 * 1000);
        // 4小时前
        Date beforeFourHours = new Date(currentDate.getTime() - 4 * 60 * 60 * 1000);
        // 6小时前
        Date beforeSixHours = new Date(currentDate.getTime() - 6 * 60 * 60 * 1000);
        // 8小时前
        Date beforeEightHours = new Date(currentDate.getTime() - 8 * 60 * 60 * 1000);
        // 10小时前
        Date beforeTenHours = new Date(currentDate.getTime() - 10 * 60 * 60 * 1000);

        // 统计返回结果Map key:时间 value:数量
        Map<Date, Integer> resultMap = Maps.newHashMap();

        // 根据时间组装报表需要的信息
        for (GengzpMqMessage message : gengzpMqMessages) {
            if (message.getCreatedTime().before(beforeTenHours)) {
                continue;
            }
            if (message.getCreatedTime().before(beforeEightHours)) {
                resultMap.put(beforeEightHours, resultMap.getOrDefault(beforeEightHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeSixHours)) {
                resultMap.put(beforeSixHours, resultMap.getOrDefault(beforeSixHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeFourHours)) {
                resultMap.put(beforeFourHours, resultMap.getOrDefault(beforeFourHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeTwoHours)) {
                resultMap.put(beforeTwoHours, resultMap.getOrDefault(beforeTwoHours, 0) + 1);
                continue;
            }
            resultMap.put(currentDate, resultMap.getOrDefault(currentDate, 0) + 1);
        }

        // 取出所有key, 按照时间从远到近排序
        ArrayList<Date> keys = Lists.newArrayList(resultMap.keySet());
        keys.sort(Comparator.naturalOrder());
        keys.forEach(v -> result.add(
                new SearchMessageCountResp(GengzpMqDateUtils.Date2String(v, "yyyy-MM-dd HH:mm:ss"), resultMap.get(v))));

        return result;
    }

    @Override
    public List<SearchMessageCountResp> searchDeathMessageCount() {
        List<SearchMessageCountResp> result = Lists.newArrayList();

        // 获取当前系统时间
        Date currentDate = new Date();

        // 往前查询10小时
        List<GengzpMqMessage> deathMessages = gengzpMqFacadeService.searchDeathMessageByCreatedTime(
                new Date(currentDate.getTime() - 10 * 60 * 60 * 1000), currentDate);
        if (CollectionUtils.isEmpty(deathMessages)) {
            return result;
        }

        // 2小时前
        Date beforeTwoHours = new Date(currentDate.getTime() - 2 * 60 * 60 * 1000);
        // 4小时前
        Date beforeFourHours = new Date(currentDate.getTime() - 4 * 60 * 60 * 1000);
        // 6小时前
        Date beforeSixHours = new Date(currentDate.getTime() - 6 * 60 * 60 * 1000);
        // 8小时前
        Date beforeEightHours = new Date(currentDate.getTime() - 8 * 60 * 60 * 1000);
        // 10小时前
        Date beforeTenHours = new Date(currentDate.getTime() - 10 * 60 * 60 * 1000);

        // 统计返回结果Map key:时间 value:数量
        Map<Date, Integer> resultMap = Maps.newHashMap();

        // 根据时间组装报表需要的信息
        for (GengzpMqMessage message : deathMessages) {
            if (message.getCreatedTime().before(beforeTenHours)) {
                continue;
            }
            if (message.getCreatedTime().before(beforeEightHours)) {
                resultMap.put(beforeEightHours, resultMap.getOrDefault(beforeEightHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeSixHours)) {
                resultMap.put(beforeSixHours, resultMap.getOrDefault(beforeSixHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeFourHours)) {
                resultMap.put(beforeFourHours, resultMap.getOrDefault(beforeFourHours, 0) + 1);
                continue;
            }
            if (message.getCreatedTime().before(beforeTwoHours)) {
                resultMap.put(beforeTwoHours, resultMap.getOrDefault(beforeTwoHours, 0) + 1);
                continue;
            }
            resultMap.put(currentDate, resultMap.getOrDefault(currentDate, 0) + 1);
        }

        // 取出所有key, 按照时间从远到近排序
        ArrayList<Date> keys = Lists.newArrayList(resultMap.keySet());
        keys.sort(Comparator.naturalOrder());
        keys.forEach(v -> result.add(
                new SearchMessageCountResp(GengzpMqDateUtils.Date2String(v, "yyyy-MM-dd HH:mm:ss"), resultMap.get(v))));

        return result;
    }

}
